package com.guangpan.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guangpan.springboot.model.UserInfo;
import com.guangpan.springboot.service.TaskService;
import com.guangpan.springboot.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class UserinfoController {

    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    TaskService taskService;

    @Value("${web.upload-path}")
    private String path;

    /**
     * 登录
     * @param userId 输入你的学号
     * @param passWord 输入你设置好的密码
     * @return
     */
    @RequestMapping("/login")
    public @ResponseBody String userLogin(@RequestParam("userId")String userId, @RequestParam("passWord")String passWord){
        UserInfo userinfo_p=new UserInfo();
        userinfo_p.setUserId(userId);
        userinfo_p.setUserPassword(passWord);

        UserInfo userinfo=userinfoService.loginUser(userinfo_p);

        String result=userinfo.getId()+"|"+userinfo.getUserName();

        return result;
    }

    /**
     * 注册
     * @param userId 输入你的学号
     * @param userName 输入你的姓名
     * @param password 输入你设置的密码
     * @return
     */
    @RequestMapping("/register")
    public @ResponseBody Integer userRegister(@RequestParam("userId")String userId,@RequestParam("userName")String userName,@RequestParam("passWord")String password){
        UserInfo userinfo_p=new UserInfo();
        userinfo_p.setUserId(userId);
        userinfo_p.setUserName(userName);
        userinfo_p.setUserPassword(password);

        Integer result = userinfoService.registerUser(userinfo_p);

        return result;
    }

    /**
     * 跳转到文件上传页面
     * @return
     */
    @RequestMapping("/upload")
    public String toUpload(){
        //返回"toUpload.ftlh页面
        return "toUpload";
    }


    /**
     * 上传文件
     * @param file 要上传的文件
     * @return
     */
    @RequestMapping("/fileUpload")
    public String upload(@RequestParam("fileName")MultipartFile file){
        //要上传的目标文件存放路径
        String localPath = path;
        //上传成功或者失败的提示
        String msg = "";
        //产生随机值，用于测试，后续需要修改 从用户处读取其ID
        String userId= UUID.randomUUID().toString();

        String fileName = taskService.getFileName(file.getOriginalFilename());
        System.out.println(fileName);
        try{
            taskService.upload(file,localPath,fileName);
        } catch (IOException e) {
            msg = "上传失败！";
            e.getStackTrace();
            return msg;
        }
        msg = "上传成功!";

        taskService.transfer(fileName,userId);
        System.out.println(msg);
        Map<String,Object> map2=new HashMap<>();
        //生成日期
        Date currentTime = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String date = simpleDateFormat.format(currentTime);
        map2.put("发送日期:",date);
        map2.put("发送用户:",userId);
        map2.put("发送图片名:",fileName);

        return new JSONObject(map2).toString();
    }

    /**
     * 我的信息
     * @param userId
     * @return
     */
    @RequestMapping("/userInfo")
    public String getUserInfo(@RequestParam("userId")String userId){
        //通过userId从数据库中查询用户的光盘书以及用户当前积分
        Map<String ,Object> map=new HashMap<>();
        //以下同于测试
        map.put("emptyCount",1);
        map.put("points",100);
        return new JSONObject(map).toString();
    }



    /**
     * 积分兑换
     * @param userId
     * @param type 0代表选择兑换0.5学分；1代表选择兑换1学分；2代表选择兑换2学分；
     * @return
     */
    @RequestMapping("/exchange")
    public String exchange(@RequestParam("userId")String userId,@RequestParam("type")Integer type){
        //通过数据库查询，减库存等操作
        Map<String , Object> map=new HashMap<>();
        //以下用于测试
        map.put("res","success");
        return new JSONObject(map).toString();
    }


    /**
     * 兑换记录
     */
    @RequestMapping("/record")
    public String record(@RequestParam("userId")String userId){
        //通过数据库查询

        //以下用于测试
        List<record> list = new ArrayList<>();

        record e1 = new record();
        e1.setDate("2021.6.3");
        e1.setPoints(100);
        list.add(e1);

        record e2 = new record();
        e1.setDate("2021.6.2");
        e1.setPoints(200);
        list.add(e2);

        JSONArray array = JSON.parseArray(JSON.toJSONString(list));
        System.out.println("JSONArray为："+array);

        return array.toString();

    }







}
